import { History } from "./base"

function getHash() {
    return window.location.hash.slice(1)
}
class HashHistory extends History {
    constructor(router) {
        super(router)
        this.router = router
        //确保是hash模式
        ensureSlash()
    }
    getCurrentLocation() {
        return getHash()
    }
    setUpLister() {
        window.addEventListener('hashchange', () => {
            //路由改变时跳转到最近的路径
            this.transitionTo(getHash())
        })
    }
}
function ensureSlash() {
    if (window.location.hash) {
        return
    }
    window.location.hash = '/'
}
export {
    HashHistory
}